package com.itheima.controller.backend.security;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.dto.CheckItemDTO;
import com.itheima.dto.CheckItemPageQuertDTO;
import com.itheima.exception.BusinessException;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import com.itheima.vo.CheckItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;



    /**
     * 添加检查项
     * @param checkItemDTO
     */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")//权限校验

    @PostMapping("/add")
    public Result add(@RequestBody CheckItemDTO checkItemDTO){
        log.info("添加检查项请求：code={}, name={}",
                checkItemDTO.getCode(), checkItemDTO.getName());
        checkItemService.add(checkItemDTO);
        return new Result(true,"新增检查项成功");
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error("添加检查项失败", e);
        return new Result(false, "新增检查项失败：" + e.getMessage());
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验

    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("检查项分页查询，参数为：{}",queryPageBean);
        PageResult pageResult = checkItemService.findPage(queryPageBean);
        return new Result(true,"查询成功",pageResult);
    }

    /**
     * 根据id删除检查项
     * ①根据id查询信息，用于回显（更新）
     * ②根据id删除信息
     */

    /**
     * 根据id查询检查项
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验

    @GetMapping("/findById")
    public Result<CheckItem> findById(@RequestParam Integer id){
        log.info("根据id查询检查项：{}",id);
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true,"查询成功",checkItem);
    }
    /**
     * 根据id删除检查项
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        log.info("根据id删除检查项：{}", id);
        if (id == null || id <= 0) {
            return new Result(false, "无效的检查项ID");
        }

        try {
            checkItemService.delete(id);
            return new Result(true, "删除成功");
        } catch (IllegalArgumentException e) {
            log.warn("删除检查项失败：{}", e.getMessage());
            return new Result(false, e.getMessage());
        } catch (BusinessException e) {
            log.warn("删除检查项失败：{}", e.getMessage());
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            log.error("删除检查项失败", e);
            return new Result(false, "删除检查项失败：" + e.getMessage());
        }
    }

    /**
     * 更新检查项
     */
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")//权限校验

    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItemDTO checkItemDTO){
        log.info("更新检查项：{}",checkItemDTO);
        checkItemService.edit(checkItemDTO);
        return new Result(true,"更新成功");
    }
    /**
     * 查询所有的检查项(添加检查组中的检查项)
     */
    @GetMapping("/findAll.do")
    public Result findAll(){
        try {
            List<CheckItem> lists = checkItemService.findAll();
            return new Result(true,"查询检查项成功",lists);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询检查项失败");
        }
    }
}
